photoHelper.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. *
  3. * @authors Eric Hsiao
  4. *
  5. */
  6. var photoHelper = function () {
  7. //private menbers
  8. var photoData;
  9. var maxHeight = 1200;
  10. //private methods
  11. function init() {
  12. console.log('photoHelper is loaded.');
  13. }
  14. function loadFile(_input,_callback) {
  15. // var file = document.querySelector('input[type=file]').files[0];
  16. var file = $(_input)[0].files[0];
  17. ImageHelper.resizeAndRotateImage(file,maxHeight,function(resizeImageObj){
  18. // var newImage = new Image();
  19. // newImage.src = resizeImageObj;
  20. _callback(resizeImageObj);
  21. });
  22. }
  23. var ImageHelper = {
  24. resizeAndRotateImage: function (inImageSource, inMaxLength, inSuccessCallback) {
  25. var reader = new FileReader();
  26. reader.readAsDataURL(inImageSource);
  27. reader.onload = function (e) {
  28. var canvas = document.createElement('canvas');
  29. var ctx = canvas.getContext("2d");
  30. var img = new Image();
  31. img.onload = function () {
  32. //設定長邊上限值
  33. var max_Length = inMaxLength;
  34. var imgWidth = img.width;
  35. var imgHeight = img.height;
  36. if (imgWidth > imgHeight) {
  37. if (imgHeight > max_Length) {
  38. imgWidth = Math.round(imgWidth *= max_Length / imgHeight);
  39. imgHeight = max_Length;
  40. }
  41. } else {
  42. if (imgWidth > max_Length) {
  43. imgHeight = Math.round(imgHeight *= max_Length / imgWidth);
  44. imgWidth = max_Length;
  45. }
  46. }
  47. canvas.width = imgWidth;
  48. canvas.height = imgHeight;
  49. var that = this;
  50. EXIF.getData(img, function () {
  51. var orientation = EXIF.getTag(that, 'Orientation');
  52. // alert(orientation);
  53. if (orientation == 6 || orientation == 8 || orientation == 3) {
  54. var rotateAngle = 0;
  55. switch (orientation) {
  56. case 3:
  57. rotateAngle = 180;
  58. break;
  59. case 6:
  60. rotateAngle = 90;
  61. canvas.width = imgHeight;
  62. canvas.height = imgWidth;
  63. break;
  64. case 8:
  65. rotateAngle = -90;
  66. canvas.width = imgHeight;
  67. canvas.height = imgWidth;
  68. break;
  69. }
  70. var x = canvas.width / 2;
  71. var y = canvas.height / 2;
  72. ctx.translate(x, y);
  73. ctx.rotate(rotateAngle * Math.PI / 180);
  74. ctx.drawImage(img, (-imgWidth / 2), (-imgHeight / 2), imgWidth, imgHeight);
  75. console.log(imgWidth + ', ' + imgHeight);
  76. }
  77. else {
  78. ctx.drawImage(img, 0, 0, imgWidth, imgHeight);
  79. console.log(imgWidth + ', ' + imgHeight);
  80. }
  81. });
  82. var res = canvas.toDataURL("image/jpeg", 0.9);
  83. inSuccessCallback(res);
  84. };
  85. img.src = e.target.result;
  86. };
  87. }
  88. };
  89. //constructor
  90. {
  91. $(document).ready(function () {
  92. init();
  93. });
  94. }
  95. //public
  96. return {
  97. loadFile: function (_input,_callback) {
  98. loadFile(_input,_callback);
  99. }
  100. };
  101. };